﻿using System.Drawing;

namespace Jayden.Dll.Tetris
{
	public class BlockDefinition
	{

		private int m_Width;
		private int m_Height;
		private uint m_Shape;
		private Color m_Color;

		public static readonly BlockDefinition I = new BlockDefinition(4, 1, "XXXX", Color.Red);
		public static readonly BlockDefinition J = new BlockDefinition(3, 2, "XXX" + "  X", Color.Orange);
		public static readonly BlockDefinition L = new BlockDefinition(3, 2, "XXX" + "X  ", Color.Magenta);
		public static readonly BlockDefinition O = new BlockDefinition(2, 2, "XX" + "XX", Color.Blue);
		public static readonly BlockDefinition S = new BlockDefinition(3, 2, " XX" + "XX ", Color.Green);
		public static readonly BlockDefinition T = new BlockDefinition(3, 2, "XXX" + " X ", Color.Olive);
		public static readonly BlockDefinition Z = new BlockDefinition(3, 2, "XX " + " XX", Color.Cyan);

		public BlockDefinition(int width, int height, string shape, Color color)
		{
			m_Width = width;
			m_Height = height;
			m_Color = color;
			for (int c = 0; c < width; c++)
				for (int d = 0; d < height; d++)
					SetBlock(c, d, shape[c + d * width] != ' ');
		}

		public void SetBlock(int x, int y, bool isBlock)
		{
			int offset = x + y * m_Width;
			uint mask = (uint)(1 << offset);
			if (isBlock)
				m_Shape = m_Shape | mask;
			else
				m_Shape = m_Shape & (~mask);
		}

		public bool IsBlock(int x, int y)
		{
			int offset = x + y * m_Width;
			uint mask = (uint)(1 << offset);
			return (m_Shape & mask) != 0;
		}

		public int Width { get { return m_Width; } }
		public int Height { get { return m_Height; } }
		public Color Color { get { return m_Color; } }
	}
}
